package com.spyz.P2_combine_file_inputformat_wordcount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineFileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCountDrive {
    public static void main(String[] args) throws Exception {
        // 1、获取job
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "word count");

        // 2、获取jar包路径
        job.setJarByClass(WordCountDrive.class);

        // 3、关联mapper和reduce
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReduce.class);

        // 4、设置mapper的KV类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        // 5、设置最终的输出的KV类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 设置combinerTextOutputFormat
        // 默认是TextOutputFormat
        job.setInputFormatClass(CombineTextInputFormat.class);
        // 设置虚拟存储空间大小
        CombineFileInputFormat.setMaxInputSplitSize(job, 4194304);// 4M

        // 6、设置输入路径和输出路径
        FileInputFormat.setInputPaths(job, new Path("./input"));
        FileOutputFormat.setOutputPath(job, new Path("./output/wc"));

        // 7、提交job
        boolean result = job.waitForCompletion(true);

        System.exit(result ? 0 : 1);
    }
}
